<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>django.core.urlresolvers &mdash; Django 1.7.8.dev20150401230226 documentation</title>
    
    <link rel="stylesheet" href="../../../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../../../',
        VERSION:     '1.7.8.dev20150401230226',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../../_static/doctools.js"></script>
    <link rel="top" title="Django 1.7.8.dev20150401230226 documentation" href="../../../index.html" />
    <link rel="up" title="django" href="../../django.html" />



 
<script type="text/javascript" src="../../../templatebuiltins.js"></script>
<script type="text/javascript">
(function($) {
    if (!django_template_builtins) {
       // templatebuiltins.js missing, do nothing.
       return;
    }
    $(document).ready(function() {
        // Hyperlink Django template tags and filters
        var base = "../../../ref/templates/builtins.html";
        if (base == "#") {
            // Special case for builtins.html itself
            base = "";
        }
        // Tags are keywords, class '.k'
        $("div.highlight\\-html\\+django span.k").each(function(i, elem) {
             var tagname = $(elem).text();
             if ($.inArray(tagname, django_template_builtins.ttags) != -1) {
                 var fragment = tagname.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + tagname + "</a>");
             }
        });
        // Filters are functions, class '.nf'
        $("div.highlight\\-html\\+django span.nf").each(function(i, elem) {
             var filtername = $(elem).text();
             if ($.inArray(filtername, django_template_builtins.tfilters) != -1) {
                 var fragment = filtername.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + filtername + "</a>");
             }
        });
    });
})(jQuery);
</script>


  </head>
  <body>

    <div class="document">
  <div id="custom-doc" class="yui-t6">
    <div id="hd">
      <h1><a href="../../../index.html">Django 1.7.8.dev20150401230226 documentation</a></h1>
      <div id="global-nav">
        <a title="Home page" href="../../../index.html">Home</a>  |
        <a title="Table of contents" href="../../../contents.html">Table of contents</a>  |
        <a title="Global index" href="../../../genindex.html">Index</a>  |
        <a title="Module index" href="../../../py-modindex.html">Modules</a>
      </div>
      <div class="nav">
    <a href="../../index.html" title="Module code" accesskey="U">up</a></div>
    </div>

    <div id="bd">
      <div id="yui-main">
        <div class="yui-b">
          <div class="yui-g" id="_modules-django-core-urlresolvers">
            
  <h1>Source code for django.core.urlresolvers</h1><div class="highlight"><pre>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd">This module converts requested URLs to callback view functions.</span>

<span class="sd">RegexURLResolver is the main class here. Its resolve() method takes a URL (as</span>
<span class="sd">a string) and returns a tuple in this format:</span>

<span class="sd">    (view_function, function_args, function_kwargs)</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="kn">from</span> <span class="nn">__future__</span> <span class="kn">import</span> <span class="n">unicode_literals</span>

<span class="kn">import</span> <span class="nn">functools</span>
<span class="kn">from</span> <span class="nn">importlib</span> <span class="kn">import</span> <span class="n">import_module</span>
<span class="kn">import</span> <span class="nn">re</span>
<span class="kn">from</span> <span class="nn">threading</span> <span class="kn">import</span> <span class="n">local</span>

<span class="kn">from</span> <span class="nn">django.http</span> <span class="kn">import</span> <span class="n">Http404</span>
<span class="kn">from</span> <span class="nn">django.core.exceptions</span> <span class="kn">import</span> <span class="n">ImproperlyConfigured</span><span class="p">,</span> <span class="n">ViewDoesNotExist</span>
<span class="kn">from</span> <span class="nn">django.utils.datastructures</span> <span class="kn">import</span> <span class="n">MultiValueDict</span>
<span class="kn">from</span> <span class="nn">django.utils.encoding</span> <span class="kn">import</span> <span class="n">force_str</span><span class="p">,</span> <span class="n">force_text</span><span class="p">,</span> <span class="n">iri_to_uri</span>
<span class="kn">from</span> <span class="nn">django.utils.functional</span> <span class="kn">import</span> <span class="n">lazy</span>
<span class="kn">from</span> <span class="nn">django.utils.http</span> <span class="kn">import</span> <span class="n">urlquote</span>
<span class="kn">from</span> <span class="nn">django.utils.module_loading</span> <span class="kn">import</span> <span class="n">module_has_submodule</span>
<span class="kn">from</span> <span class="nn">django.utils.regex_helper</span> <span class="kn">import</span> <span class="n">normalize</span>
<span class="kn">from</span> <span class="nn">django.utils</span> <span class="kn">import</span> <span class="n">six</span><span class="p">,</span> <span class="n">lru_cache</span>
<span class="kn">from</span> <span class="nn">django.utils.translation</span> <span class="kn">import</span> <span class="n">get_language</span>


<span class="c"># SCRIPT_NAME prefixes for each thread are stored here. If there&#39;s no entry for</span>
<span class="c"># the current thread (which is the only one we ever access), it is assumed to</span>
<span class="c"># be empty.</span>
<span class="n">_prefixes</span> <span class="o">=</span> <span class="n">local</span><span class="p">()</span>

<span class="c"># Overridden URLconfs for each thread are stored here.</span>
<span class="n">_urlconfs</span> <span class="o">=</span> <span class="n">local</span><span class="p">()</span>


<div class="viewcode-block" id="ResolverMatch"><a class="viewcode-back" href="../../../ref/urlresolvers.html#django.core.urlresolvers.ResolverMatch">[docs]</a><span class="k">class</span> <span class="nc">ResolverMatch</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">func</span><span class="p">,</span> <span class="n">args</span><span class="p">,</span> <span class="n">kwargs</span><span class="p">,</span> <span class="n">url_name</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">app_name</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">namespaces</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">func</span> <span class="o">=</span> <span class="n">func</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">args</span> <span class="o">=</span> <span class="n">args</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">kwargs</span> <span class="o">=</span> <span class="n">kwargs</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">app_name</span> <span class="o">=</span> <span class="n">app_name</span>
        <span class="k">if</span> <span class="n">namespaces</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">namespaces</span> <span class="o">=</span> <span class="p">[</span><span class="n">x</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">namespaces</span> <span class="k">if</span> <span class="n">x</span><span class="p">]</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">namespaces</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">url_name</span><span class="p">:</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">func</span><span class="p">,</span> <span class="s">&#39;__name__&#39;</span><span class="p">):</span>
                <span class="c"># An instance of a callable class</span>
                <span class="n">url_name</span> <span class="o">=</span> <span class="s">&#39;.&#39;</span><span class="o">.</span><span class="n">join</span><span class="p">([</span><span class="n">func</span><span class="o">.</span><span class="n">__class__</span><span class="o">.</span><span class="n">__module__</span><span class="p">,</span> <span class="n">func</span><span class="o">.</span><span class="n">__class__</span><span class="o">.</span><span class="n">__name__</span><span class="p">])</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="c"># A function</span>
                <span class="n">url_name</span> <span class="o">=</span> <span class="s">&#39;.&#39;</span><span class="o">.</span><span class="n">join</span><span class="p">([</span><span class="n">func</span><span class="o">.</span><span class="n">__module__</span><span class="p">,</span> <span class="n">func</span><span class="o">.</span><span class="n">__name__</span><span class="p">])</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">url_name</span> <span class="o">=</span> <span class="n">url_name</span>

    <span class="nd">@property</span>
<div class="viewcode-block" id="ResolverMatch.namespace"><a class="viewcode-back" href="../../../ref/urlresolvers.html#django.core.urlresolvers.ResolverMatch.namespace">[docs]</a>    <span class="k">def</span> <span class="nf">namespace</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="s">&#39;:&#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">namespaces</span><span class="p">)</span>
</div>
    <span class="nd">@property</span>
<div class="viewcode-block" id="ResolverMatch.view_name"><a class="viewcode-back" href="../../../ref/urlresolvers.html#django.core.urlresolvers.ResolverMatch.view_name">[docs]</a>    <span class="k">def</span> <span class="nf">view_name</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="s">&#39;:&#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="nb">filter</span><span class="p">(</span><span class="nb">bool</span><span class="p">,</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">namespace</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">url_name</span><span class="p">)))</span>
</div>
    <span class="k">def</span> <span class="nf">__getitem__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">index</span><span class="p">):</span>
        <span class="k">return</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">func</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">args</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">kwargs</span><span class="p">)[</span><span class="n">index</span><span class="p">]</span>

    <span class="k">def</span> <span class="nf">__repr__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="s">&quot;ResolverMatch(func=</span><span class="si">%s</span><span class="s">, args=</span><span class="si">%s</span><span class="s">, kwargs=</span><span class="si">%s</span><span class="s">, url_name=&#39;</span><span class="si">%s</span><span class="s">&#39;, app_name=&#39;</span><span class="si">%s</span><span class="s">&#39;, namespace=&#39;</span><span class="si">%s</span><span class="s">&#39;)&quot;</span> <span class="o">%</span> <span class="p">(</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">func</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">args</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">kwargs</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">url_name</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">app_name</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">namespace</span><span class="p">)</span>

</div>
<div class="viewcode-block" id="Resolver404"><a class="viewcode-back" href="../../../ref/exceptions.html#django.core.urlresolvers.Resolver404">[docs]</a><span class="k">class</span> <span class="nc">Resolver404</span><span class="p">(</span><span class="n">Http404</span><span class="p">):</span>
    <span class="k">pass</span>

</div>
<div class="viewcode-block" id="NoReverseMatch"><a class="viewcode-back" href="../../../ref/exceptions.html#django.core.urlresolvers.NoReverseMatch">[docs]</a><span class="k">class</span> <span class="nc">NoReverseMatch</span><span class="p">(</span><span class="ne">Exception</span><span class="p">):</span>
    <span class="k">pass</span>

</div>
<span class="nd">@lru_cache.lru_cache</span><span class="p">(</span><span class="n">maxsize</span><span class="o">=</span><span class="bp">None</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">get_callable</span><span class="p">(</span><span class="n">lookup_view</span><span class="p">,</span> <span class="n">can_fail</span><span class="o">=</span><span class="bp">False</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Convert a string version of a function name to the callable object.</span>

<span class="sd">    If the lookup_view is not an import path, it is assumed to be a URL pattern</span>
<span class="sd">    label and the original string is returned.</span>

<span class="sd">    If can_fail is True, lookup_view might be a URL pattern label, so errors</span>
<span class="sd">    during the import fail and the string is returned.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">if</span> <span class="ow">not</span> <span class="nb">callable</span><span class="p">(</span><span class="n">lookup_view</span><span class="p">):</span>
        <span class="n">mod_name</span><span class="p">,</span> <span class="n">func_name</span> <span class="o">=</span> <span class="n">get_mod_func</span><span class="p">(</span><span class="n">lookup_view</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">func_name</span> <span class="o">==</span> <span class="s">&#39;&#39;</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">lookup_view</span>

        <span class="k">try</span><span class="p">:</span>
            <span class="n">mod</span> <span class="o">=</span> <span class="n">import_module</span><span class="p">(</span><span class="n">mod_name</span><span class="p">)</span>
        <span class="k">except</span> <span class="ne">ImportError</span><span class="p">:</span>
            <span class="n">parentmod</span><span class="p">,</span> <span class="n">submod</span> <span class="o">=</span> <span class="n">get_mod_func</span><span class="p">(</span><span class="n">mod_name</span><span class="p">)</span>
            <span class="k">if</span> <span class="p">(</span><span class="ow">not</span> <span class="n">can_fail</span> <span class="ow">and</span> <span class="n">submod</span> <span class="o">!=</span> <span class="s">&#39;&#39;</span> <span class="ow">and</span>
                    <span class="ow">not</span> <span class="n">module_has_submodule</span><span class="p">(</span><span class="n">import_module</span><span class="p">(</span><span class="n">parentmod</span><span class="p">),</span> <span class="n">submod</span><span class="p">)):</span>
                <span class="k">raise</span> <span class="n">ViewDoesNotExist</span><span class="p">(</span>
                    <span class="s">&quot;Could not import </span><span class="si">%s</span><span class="s">. Parent module </span><span class="si">%s</span><span class="s"> does not exist.&quot;</span> <span class="o">%</span>
                    <span class="p">(</span><span class="n">lookup_view</span><span class="p">,</span> <span class="n">mod_name</span><span class="p">))</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="n">can_fail</span><span class="p">:</span>
                <span class="k">raise</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="n">lookup_view</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">mod</span><span class="p">,</span> <span class="n">func_name</span><span class="p">)</span>
                <span class="k">if</span> <span class="ow">not</span> <span class="nb">callable</span><span class="p">(</span><span class="n">lookup_view</span><span class="p">):</span>
                    <span class="k">raise</span> <span class="n">ViewDoesNotExist</span><span class="p">(</span>
                        <span class="s">&quot;Could not import </span><span class="si">%s</span><span class="s">.</span><span class="si">%s</span><span class="s">. View is not callable.&quot;</span> <span class="o">%</span>
                        <span class="p">(</span><span class="n">mod_name</span><span class="p">,</span> <span class="n">func_name</span><span class="p">))</span>
            <span class="k">except</span> <span class="ne">AttributeError</span><span class="p">:</span>
                <span class="k">if</span> <span class="ow">not</span> <span class="n">can_fail</span><span class="p">:</span>
                    <span class="k">raise</span> <span class="n">ViewDoesNotExist</span><span class="p">(</span>
                        <span class="s">&quot;Could not import </span><span class="si">%s</span><span class="s">. View does not exist in module </span><span class="si">%s</span><span class="s">.&quot;</span> <span class="o">%</span>
                        <span class="p">(</span><span class="n">lookup_view</span><span class="p">,</span> <span class="n">mod_name</span><span class="p">))</span>
    <span class="k">return</span> <span class="n">lookup_view</span>


<span class="nd">@lru_cache.lru_cache</span><span class="p">(</span><span class="n">maxsize</span><span class="o">=</span><span class="bp">None</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">get_resolver</span><span class="p">(</span><span class="n">urlconf</span><span class="p">):</span>
    <span class="k">if</span> <span class="n">urlconf</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
        <span class="kn">from</span> <span class="nn">django.conf</span> <span class="kn">import</span> <span class="n">settings</span>
        <span class="n">urlconf</span> <span class="o">=</span> <span class="n">settings</span><span class="o">.</span><span class="n">ROOT_URLCONF</span>
    <span class="k">return</span> <span class="n">RegexURLResolver</span><span class="p">(</span><span class="s">r&#39;^/&#39;</span><span class="p">,</span> <span class="n">urlconf</span><span class="p">)</span>


<span class="nd">@lru_cache.lru_cache</span><span class="p">(</span><span class="n">maxsize</span><span class="o">=</span><span class="bp">None</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">get_ns_resolver</span><span class="p">(</span><span class="n">ns_pattern</span><span class="p">,</span> <span class="n">resolver</span><span class="p">):</span>
    <span class="c"># Build a namespaced resolver for the given parent urlconf pattern.</span>
    <span class="c"># This makes it possible to have captured parameters in the parent</span>
    <span class="c"># urlconf pattern.</span>
    <span class="n">ns_resolver</span> <span class="o">=</span> <span class="n">RegexURLResolver</span><span class="p">(</span><span class="n">ns_pattern</span><span class="p">,</span> <span class="n">resolver</span><span class="o">.</span><span class="n">url_patterns</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">RegexURLResolver</span><span class="p">(</span><span class="s">r&#39;^/&#39;</span><span class="p">,</span> <span class="p">[</span><span class="n">ns_resolver</span><span class="p">])</span>


<span class="k">def</span> <span class="nf">get_mod_func</span><span class="p">(</span><span class="n">callback</span><span class="p">):</span>
    <span class="c"># Converts &#39;django.views.news.stories.story_detail&#39; to</span>
    <span class="c"># [&#39;django.views.news.stories&#39;, &#39;story_detail&#39;]</span>
    <span class="k">try</span><span class="p">:</span>
        <span class="n">dot</span> <span class="o">=</span> <span class="n">callback</span><span class="o">.</span><span class="n">rindex</span><span class="p">(</span><span class="s">&#39;.&#39;</span><span class="p">)</span>
    <span class="k">except</span> <span class="ne">ValueError</span><span class="p">:</span>
        <span class="k">return</span> <span class="n">callback</span><span class="p">,</span> <span class="s">&#39;&#39;</span>
    <span class="k">return</span> <span class="n">callback</span><span class="p">[:</span><span class="n">dot</span><span class="p">],</span> <span class="n">callback</span><span class="p">[</span><span class="n">dot</span> <span class="o">+</span> <span class="mi">1</span><span class="p">:]</span>


<span class="k">class</span> <span class="nc">LocaleRegexProvider</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    A mixin to provide a default regex property which can vary by active</span>
<span class="sd">    language.</span>

<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">regex</span><span class="p">):</span>
        <span class="c"># regex is either a string representing a regular expression, or a</span>
        <span class="c"># translatable string (using ugettext_lazy) representing a regular</span>
        <span class="c"># expression.</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_regex</span> <span class="o">=</span> <span class="n">regex</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_regex_dict</span> <span class="o">=</span> <span class="p">{}</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">regex</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Returns a compiled regular expression, depending upon the activated</span>
<span class="sd">        language-code.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">language_code</span> <span class="o">=</span> <span class="n">get_language</span><span class="p">()</span>
        <span class="k">if</span> <span class="n">language_code</span> <span class="ow">not</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_regex_dict</span><span class="p">:</span>
            <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_regex</span><span class="p">,</span> <span class="n">six</span><span class="o">.</span><span class="n">string_types</span><span class="p">):</span>
                <span class="n">regex</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_regex</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">regex</span> <span class="o">=</span> <span class="n">force_text</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_regex</span><span class="p">)</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="n">compiled_regex</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">compile</span><span class="p">(</span><span class="n">regex</span><span class="p">,</span> <span class="n">re</span><span class="o">.</span><span class="n">UNICODE</span><span class="p">)</span>
            <span class="k">except</span> <span class="n">re</span><span class="o">.</span><span class="n">error</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
                <span class="k">raise</span> <span class="n">ImproperlyConfigured</span><span class="p">(</span>
                    <span class="s">&#39;&quot;</span><span class="si">%s</span><span class="s">&quot; is not a valid regular expression: </span><span class="si">%s</span><span class="s">&#39;</span> <span class="o">%</span>
                    <span class="p">(</span><span class="n">regex</span><span class="p">,</span> <span class="n">six</span><span class="o">.</span><span class="n">text_type</span><span class="p">(</span><span class="n">e</span><span class="p">)))</span>

            <span class="bp">self</span><span class="o">.</span><span class="n">_regex_dict</span><span class="p">[</span><span class="n">language_code</span><span class="p">]</span> <span class="o">=</span> <span class="n">compiled_regex</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_regex_dict</span><span class="p">[</span><span class="n">language_code</span><span class="p">]</span>


<span class="k">class</span> <span class="nc">RegexURLPattern</span><span class="p">(</span><span class="n">LocaleRegexProvider</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">regex</span><span class="p">,</span> <span class="n">callback</span><span class="p">,</span> <span class="n">default_args</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
        <span class="n">LocaleRegexProvider</span><span class="o">.</span><span class="n">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">regex</span><span class="p">)</span>
        <span class="c"># callback is either a string like &#39;foo.views.news.stories.story_detail&#39;</span>
        <span class="c"># which represents the path to a module and a view function name, or a</span>
        <span class="c"># callable object (view).</span>
        <span class="k">if</span> <span class="nb">callable</span><span class="p">(</span><span class="n">callback</span><span class="p">):</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_callback</span> <span class="o">=</span> <span class="n">callback</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_callback</span> <span class="o">=</span> <span class="bp">None</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_callback_str</span> <span class="o">=</span> <span class="n">callback</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">default_args</span> <span class="o">=</span> <span class="n">default_args</span> <span class="ow">or</span> <span class="p">{}</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">name</span> <span class="o">=</span> <span class="n">name</span>

    <span class="k">def</span> <span class="nf">__repr__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">force_str</span><span class="p">(</span><span class="s">&#39;&lt;</span><span class="si">%s</span><span class="s"> </span><span class="si">%s</span><span class="s"> </span><span class="si">%s</span><span class="s">&gt;&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">__class__</span><span class="o">.</span><span class="n">__name__</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">regex</span><span class="o">.</span><span class="n">pattern</span><span class="p">))</span>

    <span class="k">def</span> <span class="nf">add_prefix</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">prefix</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Adds the prefix string to a string-based callback.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">prefix</span> <span class="ow">or</span> <span class="ow">not</span> <span class="nb">hasattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="s">&#39;_callback_str&#39;</span><span class="p">):</span>
            <span class="k">return</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_callback_str</span> <span class="o">=</span> <span class="n">prefix</span> <span class="o">+</span> <span class="s">&#39;.&#39;</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">_callback_str</span>

    <span class="k">def</span> <span class="nf">resolve</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">path</span><span class="p">):</span>
        <span class="n">match</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">regex</span><span class="o">.</span><span class="n">search</span><span class="p">(</span><span class="n">path</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">match</span><span class="p">:</span>
            <span class="c"># If there are any named groups, use those as kwargs, ignoring</span>
            <span class="c"># non-named groups. Otherwise, pass all non-named arguments as</span>
            <span class="c"># positional arguments.</span>
            <span class="n">kwargs</span> <span class="o">=</span> <span class="n">match</span><span class="o">.</span><span class="n">groupdict</span><span class="p">()</span>
            <span class="k">if</span> <span class="n">kwargs</span><span class="p">:</span>
                <span class="n">args</span> <span class="o">=</span> <span class="p">()</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">args</span> <span class="o">=</span> <span class="n">match</span><span class="o">.</span><span class="n">groups</span><span class="p">()</span>
            <span class="c"># In both cases, pass any extra_kwargs as **kwargs.</span>
            <span class="n">kwargs</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">default_args</span><span class="p">)</span>

            <span class="k">return</span> <span class="n">ResolverMatch</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">callback</span><span class="p">,</span> <span class="n">args</span><span class="p">,</span> <span class="n">kwargs</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">)</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">callback</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_callback</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_callback</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">_callback</span> <span class="o">=</span> <span class="n">get_callable</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_callback_str</span><span class="p">)</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_callback</span>


<span class="k">class</span> <span class="nc">RegexURLResolver</span><span class="p">(</span><span class="n">LocaleRegexProvider</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">regex</span><span class="p">,</span> <span class="n">urlconf_name</span><span class="p">,</span> <span class="n">default_kwargs</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">app_name</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">namespace</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
        <span class="n">LocaleRegexProvider</span><span class="o">.</span><span class="n">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">regex</span><span class="p">)</span>
        <span class="c"># urlconf_name is a string representing the module containing URLconfs.</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">urlconf_name</span> <span class="o">=</span> <span class="n">urlconf_name</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">urlconf_name</span><span class="p">,</span> <span class="n">six</span><span class="o">.</span><span class="n">string_types</span><span class="p">):</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_urlconf_module</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">urlconf_name</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">callback</span> <span class="o">=</span> <span class="bp">None</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">default_kwargs</span> <span class="o">=</span> <span class="n">default_kwargs</span> <span class="ow">or</span> <span class="p">{}</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">namespace</span> <span class="o">=</span> <span class="n">namespace</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">app_name</span> <span class="o">=</span> <span class="n">app_name</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_reverse_dict</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_namespace_dict</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_app_dict</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="c"># set of dotted paths to all functions and classes that are used in</span>
        <span class="c"># urlpatterns</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_callback_strs</span> <span class="o">=</span> <span class="nb">set</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_populated</span> <span class="o">=</span> <span class="bp">False</span>

    <span class="k">def</span> <span class="nf">__repr__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">urlconf_name</span><span class="p">,</span> <span class="nb">list</span><span class="p">)</span> <span class="ow">and</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">urlconf_name</span><span class="p">):</span>
            <span class="c"># Don&#39;t bother to output the whole list, it can be huge</span>
            <span class="n">urlconf_repr</span> <span class="o">=</span> <span class="s">&#39;&lt;</span><span class="si">%s</span><span class="s"> list&gt;&#39;</span> <span class="o">%</span> <span class="bp">self</span><span class="o">.</span><span class="n">urlconf_name</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">__class__</span><span class="o">.</span><span class="n">__name__</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">urlconf_repr</span> <span class="o">=</span> <span class="nb">repr</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">urlconf_name</span><span class="p">)</span>
        <span class="k">return</span> <span class="nb">str</span><span class="p">(</span><span class="s">&#39;&lt;</span><span class="si">%s</span><span class="s"> </span><span class="si">%s</span><span class="s"> (</span><span class="si">%s</span><span class="s">:</span><span class="si">%s</span><span class="s">) </span><span class="si">%s</span><span class="s">&gt;&#39;</span><span class="p">)</span> <span class="o">%</span> <span class="p">(</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">__class__</span><span class="o">.</span><span class="n">__name__</span><span class="p">,</span> <span class="n">urlconf_repr</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">app_name</span><span class="p">,</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">namespace</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">regex</span><span class="o">.</span><span class="n">pattern</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">_populate</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">lookups</span> <span class="o">=</span> <span class="n">MultiValueDict</span><span class="p">()</span>
        <span class="n">namespaces</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="n">apps</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="n">language_code</span> <span class="o">=</span> <span class="n">get_language</span><span class="p">()</span>
        <span class="k">for</span> <span class="n">pattern</span> <span class="ow">in</span> <span class="nb">reversed</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">url_patterns</span><span class="p">):</span>
            <span class="k">if</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">pattern</span><span class="p">,</span> <span class="s">&#39;_callback_str&#39;</span><span class="p">):</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_callback_strs</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">pattern</span><span class="o">.</span><span class="n">_callback_str</span><span class="p">)</span>
            <span class="k">elif</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">pattern</span><span class="p">,</span> <span class="s">&#39;_callback&#39;</span><span class="p">):</span>
                <span class="n">callback</span> <span class="o">=</span> <span class="n">pattern</span><span class="o">.</span><span class="n">_callback</span>
                <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">callback</span><span class="p">,</span> <span class="n">functools</span><span class="o">.</span><span class="n">partial</span><span class="p">):</span>
                    <span class="n">callback</span> <span class="o">=</span> <span class="n">callback</span><span class="o">.</span><span class="n">func</span>

                <span class="k">if</span> <span class="ow">not</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">callback</span><span class="p">,</span> <span class="s">&#39;__name__&#39;</span><span class="p">):</span>
                    <span class="n">lookup_str</span> <span class="o">=</span> <span class="n">callback</span><span class="o">.</span><span class="n">__module__</span> <span class="o">+</span> <span class="s">&quot;.&quot;</span> <span class="o">+</span> <span class="n">callback</span><span class="o">.</span><span class="n">__class__</span><span class="o">.</span><span class="n">__name__</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">lookup_str</span> <span class="o">=</span> <span class="n">callback</span><span class="o">.</span><span class="n">__module__</span> <span class="o">+</span> <span class="s">&quot;.&quot;</span> <span class="o">+</span> <span class="n">callback</span><span class="o">.</span><span class="n">__name__</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_callback_strs</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">lookup_str</span><span class="p">)</span>
            <span class="n">p_pattern</span> <span class="o">=</span> <span class="n">pattern</span><span class="o">.</span><span class="n">regex</span><span class="o">.</span><span class="n">pattern</span>
            <span class="k">if</span> <span class="n">p_pattern</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s">&#39;^&#39;</span><span class="p">):</span>
                <span class="n">p_pattern</span> <span class="o">=</span> <span class="n">p_pattern</span><span class="p">[</span><span class="mi">1</span><span class="p">:]</span>
            <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">pattern</span><span class="p">,</span> <span class="n">RegexURLResolver</span><span class="p">):</span>
                <span class="k">if</span> <span class="n">pattern</span><span class="o">.</span><span class="n">namespace</span><span class="p">:</span>
                    <span class="n">namespaces</span><span class="p">[</span><span class="n">pattern</span><span class="o">.</span><span class="n">namespace</span><span class="p">]</span> <span class="o">=</span> <span class="p">(</span><span class="n">p_pattern</span><span class="p">,</span> <span class="n">pattern</span><span class="p">)</span>
                    <span class="k">if</span> <span class="n">pattern</span><span class="o">.</span><span class="n">app_name</span><span class="p">:</span>
                        <span class="n">apps</span><span class="o">.</span><span class="n">setdefault</span><span class="p">(</span><span class="n">pattern</span><span class="o">.</span><span class="n">app_name</span><span class="p">,</span> <span class="p">[])</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">pattern</span><span class="o">.</span><span class="n">namespace</span><span class="p">)</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">parent_pat</span> <span class="o">=</span> <span class="n">pattern</span><span class="o">.</span><span class="n">regex</span><span class="o">.</span><span class="n">pattern</span>
                    <span class="k">for</span> <span class="n">name</span> <span class="ow">in</span> <span class="n">pattern</span><span class="o">.</span><span class="n">reverse_dict</span><span class="p">:</span>
                        <span class="k">for</span> <span class="n">matches</span><span class="p">,</span> <span class="n">pat</span><span class="p">,</span> <span class="n">defaults</span> <span class="ow">in</span> <span class="n">pattern</span><span class="o">.</span><span class="n">reverse_dict</span><span class="o">.</span><span class="n">getlist</span><span class="p">(</span><span class="n">name</span><span class="p">):</span>
                            <span class="n">new_matches</span> <span class="o">=</span> <span class="n">normalize</span><span class="p">(</span><span class="n">parent_pat</span> <span class="o">+</span> <span class="n">pat</span><span class="p">)</span>
                            <span class="n">lookups</span><span class="o">.</span><span class="n">appendlist</span><span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="p">(</span><span class="n">new_matches</span><span class="p">,</span> <span class="n">p_pattern</span> <span class="o">+</span> <span class="n">pat</span><span class="p">,</span> <span class="nb">dict</span><span class="p">(</span><span class="n">defaults</span><span class="p">,</span> <span class="o">**</span><span class="n">pattern</span><span class="o">.</span><span class="n">default_kwargs</span><span class="p">)))</span>
                    <span class="k">for</span> <span class="n">namespace</span><span class="p">,</span> <span class="p">(</span><span class="n">prefix</span><span class="p">,</span> <span class="n">sub_pattern</span><span class="p">)</span> <span class="ow">in</span> <span class="n">pattern</span><span class="o">.</span><span class="n">namespace_dict</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
                        <span class="n">namespaces</span><span class="p">[</span><span class="n">namespace</span><span class="p">]</span> <span class="o">=</span> <span class="p">(</span><span class="n">p_pattern</span> <span class="o">+</span> <span class="n">prefix</span><span class="p">,</span> <span class="n">sub_pattern</span><span class="p">)</span>
                    <span class="k">for</span> <span class="n">app_name</span><span class="p">,</span> <span class="n">namespace_list</span> <span class="ow">in</span> <span class="n">pattern</span><span class="o">.</span><span class="n">app_dict</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
                        <span class="n">apps</span><span class="o">.</span><span class="n">setdefault</span><span class="p">(</span><span class="n">app_name</span><span class="p">,</span> <span class="p">[])</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="n">namespace_list</span><span class="p">)</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">_callback_strs</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="n">pattern</span><span class="o">.</span><span class="n">_callback_strs</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">bits</span> <span class="o">=</span> <span class="n">normalize</span><span class="p">(</span><span class="n">p_pattern</span><span class="p">)</span>
                <span class="n">lookups</span><span class="o">.</span><span class="n">appendlist</span><span class="p">(</span><span class="n">pattern</span><span class="o">.</span><span class="n">callback</span><span class="p">,</span> <span class="p">(</span><span class="n">bits</span><span class="p">,</span> <span class="n">p_pattern</span><span class="p">,</span> <span class="n">pattern</span><span class="o">.</span><span class="n">default_args</span><span class="p">))</span>
                <span class="k">if</span> <span class="n">pattern</span><span class="o">.</span><span class="n">name</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span>
                    <span class="n">lookups</span><span class="o">.</span><span class="n">appendlist</span><span class="p">(</span><span class="n">pattern</span><span class="o">.</span><span class="n">name</span><span class="p">,</span> <span class="p">(</span><span class="n">bits</span><span class="p">,</span> <span class="n">p_pattern</span><span class="p">,</span> <span class="n">pattern</span><span class="o">.</span><span class="n">default_args</span><span class="p">))</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_reverse_dict</span><span class="p">[</span><span class="n">language_code</span><span class="p">]</span> <span class="o">=</span> <span class="n">lookups</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_namespace_dict</span><span class="p">[</span><span class="n">language_code</span><span class="p">]</span> <span class="o">=</span> <span class="n">namespaces</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_app_dict</span><span class="p">[</span><span class="n">language_code</span><span class="p">]</span> <span class="o">=</span> <span class="n">apps</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_populated</span> <span class="o">=</span> <span class="bp">True</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">reverse_dict</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">language_code</span> <span class="o">=</span> <span class="n">get_language</span><span class="p">()</span>
        <span class="k">if</span> <span class="n">language_code</span> <span class="ow">not</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_reverse_dict</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_populate</span><span class="p">()</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_reverse_dict</span><span class="p">[</span><span class="n">language_code</span><span class="p">]</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">namespace_dict</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">language_code</span> <span class="o">=</span> <span class="n">get_language</span><span class="p">()</span>
        <span class="k">if</span> <span class="n">language_code</span> <span class="ow">not</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_namespace_dict</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_populate</span><span class="p">()</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_namespace_dict</span><span class="p">[</span><span class="n">language_code</span><span class="p">]</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">app_dict</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">language_code</span> <span class="o">=</span> <span class="n">get_language</span><span class="p">()</span>
        <span class="k">if</span> <span class="n">language_code</span> <span class="ow">not</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_app_dict</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_populate</span><span class="p">()</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_app_dict</span><span class="p">[</span><span class="n">language_code</span><span class="p">]</span>

    <span class="k">def</span> <span class="nf">_is_callback</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">):</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">_populated</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_populate</span><span class="p">()</span>
        <span class="k">return</span> <span class="n">name</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_callback_strs</span>

    <span class="k">def</span> <span class="nf">resolve</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">path</span><span class="p">):</span>
        <span class="n">path</span> <span class="o">=</span> <span class="n">force_text</span><span class="p">(</span><span class="n">path</span><span class="p">)</span>  <span class="c"># path may be a reverse_lazy object</span>
        <span class="n">tried</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">match</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">regex</span><span class="o">.</span><span class="n">search</span><span class="p">(</span><span class="n">path</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">match</span><span class="p">:</span>
            <span class="n">new_path</span> <span class="o">=</span> <span class="n">path</span><span class="p">[</span><span class="n">match</span><span class="o">.</span><span class="n">end</span><span class="p">():]</span>
            <span class="k">for</span> <span class="n">pattern</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">url_patterns</span><span class="p">:</span>
                <span class="k">try</span><span class="p">:</span>
                    <span class="n">sub_match</span> <span class="o">=</span> <span class="n">pattern</span><span class="o">.</span><span class="n">resolve</span><span class="p">(</span><span class="n">new_path</span><span class="p">)</span>
                <span class="k">except</span> <span class="n">Resolver404</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
                    <span class="n">sub_tried</span> <span class="o">=</span> <span class="n">e</span><span class="o">.</span><span class="n">args</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&#39;tried&#39;</span><span class="p">)</span>
                    <span class="k">if</span> <span class="n">sub_tried</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span>
                        <span class="n">tried</span><span class="o">.</span><span class="n">extend</span><span class="p">([</span><span class="n">pattern</span><span class="p">]</span> <span class="o">+</span> <span class="n">t</span> <span class="k">for</span> <span class="n">t</span> <span class="ow">in</span> <span class="n">sub_tried</span><span class="p">)</span>
                    <span class="k">else</span><span class="p">:</span>
                        <span class="n">tried</span><span class="o">.</span><span class="n">append</span><span class="p">([</span><span class="n">pattern</span><span class="p">])</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="k">if</span> <span class="n">sub_match</span><span class="p">:</span>
                        <span class="n">sub_match_dict</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">(</span><span class="n">match</span><span class="o">.</span><span class="n">groupdict</span><span class="p">(),</span> <span class="o">**</span><span class="bp">self</span><span class="o">.</span><span class="n">default_kwargs</span><span class="p">)</span>
                        <span class="n">sub_match_dict</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="n">sub_match</span><span class="o">.</span><span class="n">kwargs</span><span class="p">)</span>
                        <span class="k">return</span> <span class="n">ResolverMatch</span><span class="p">(</span><span class="n">sub_match</span><span class="o">.</span><span class="n">func</span><span class="p">,</span> <span class="n">sub_match</span><span class="o">.</span><span class="n">args</span><span class="p">,</span> <span class="n">sub_match_dict</span><span class="p">,</span> <span class="n">sub_match</span><span class="o">.</span><span class="n">url_name</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">app_name</span> <span class="ow">or</span> <span class="n">sub_match</span><span class="o">.</span><span class="n">app_name</span><span class="p">,</span> <span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">namespace</span><span class="p">]</span> <span class="o">+</span> <span class="n">sub_match</span><span class="o">.</span><span class="n">namespaces</span><span class="p">)</span>
                    <span class="n">tried</span><span class="o">.</span><span class="n">append</span><span class="p">([</span><span class="n">pattern</span><span class="p">])</span>
            <span class="k">raise</span> <span class="n">Resolver404</span><span class="p">({</span><span class="s">&#39;tried&#39;</span><span class="p">:</span> <span class="n">tried</span><span class="p">,</span> <span class="s">&#39;path&#39;</span><span class="p">:</span> <span class="n">new_path</span><span class="p">})</span>
        <span class="k">raise</span> <span class="n">Resolver404</span><span class="p">({</span><span class="s">&#39;path&#39;</span><span class="p">:</span> <span class="n">path</span><span class="p">})</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">urlconf_module</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_urlconf_module</span>
        <span class="k">except</span> <span class="ne">AttributeError</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_urlconf_module</span> <span class="o">=</span> <span class="n">import_module</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">urlconf_name</span><span class="p">)</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_urlconf_module</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">url_patterns</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="c"># urlconf_module might be a valid set of patterns, so we default to it</span>
        <span class="n">patterns</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">urlconf_module</span><span class="p">,</span> <span class="s">&quot;urlpatterns&quot;</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">urlconf_module</span><span class="p">)</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="nb">iter</span><span class="p">(</span><span class="n">patterns</span><span class="p">)</span>
        <span class="k">except</span> <span class="ne">TypeError</span><span class="p">:</span>
            <span class="n">msg</span> <span class="o">=</span> <span class="p">(</span>
                <span class="s">&quot;The included urlconf &#39;{name}&#39; does not appear to have any &quot;</span>
                <span class="s">&quot;patterns in it. If you see valid patterns in the file then &quot;</span>
                <span class="s">&quot;the issue is probably caused by a circular import.&quot;</span>
            <span class="p">)</span>
            <span class="k">raise</span> <span class="n">ImproperlyConfigured</span><span class="p">(</span><span class="n">msg</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">urlconf_name</span><span class="p">))</span>
        <span class="k">return</span> <span class="n">patterns</span>

    <span class="k">def</span> <span class="nf">_resolve_special</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">view_type</span><span class="p">):</span>
        <span class="n">callback</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">urlconf_module</span><span class="p">,</span> <span class="s">&#39;handler</span><span class="si">%s</span><span class="s">&#39;</span> <span class="o">%</span> <span class="n">view_type</span><span class="p">,</span> <span class="bp">None</span><span class="p">)</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">callback</span><span class="p">:</span>
            <span class="c"># No handler specified in file; use default</span>
            <span class="c"># Lazy import, since django.urls imports this file</span>
            <span class="kn">from</span> <span class="nn">django.conf</span> <span class="kn">import</span> <span class="n">urls</span>
            <span class="n">callback</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">urls</span><span class="p">,</span> <span class="s">&#39;handler</span><span class="si">%s</span><span class="s">&#39;</span> <span class="o">%</span> <span class="n">view_type</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">get_callable</span><span class="p">(</span><span class="n">callback</span><span class="p">),</span> <span class="p">{}</span>

    <span class="k">def</span> <span class="nf">resolve400</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_resolve_special</span><span class="p">(</span><span class="s">&#39;400&#39;</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">resolve403</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_resolve_special</span><span class="p">(</span><span class="s">&#39;403&#39;</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">resolve404</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_resolve_special</span><span class="p">(</span><span class="s">&#39;404&#39;</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">resolve500</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_resolve_special</span><span class="p">(</span><span class="s">&#39;500&#39;</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">reverse</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">lookup_view</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_reverse_with_prefix</span><span class="p">(</span><span class="n">lookup_view</span><span class="p">,</span> <span class="s">&#39;&#39;</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">_reverse_with_prefix</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">lookup_view</span><span class="p">,</span> <span class="n">_prefix</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">args</span> <span class="ow">and</span> <span class="n">kwargs</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s">&quot;Don&#39;t mix *args and **kwargs in call to reverse()!&quot;</span><span class="p">)</span>
        <span class="n">text_args</span> <span class="o">=</span> <span class="p">[</span><span class="n">force_text</span><span class="p">(</span><span class="n">v</span><span class="p">)</span> <span class="k">for</span> <span class="n">v</span> <span class="ow">in</span> <span class="n">args</span><span class="p">]</span>
        <span class="n">text_kwargs</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">((</span><span class="n">k</span><span class="p">,</span> <span class="n">force_text</span><span class="p">(</span><span class="n">v</span><span class="p">))</span> <span class="k">for</span> <span class="p">(</span><span class="n">k</span><span class="p">,</span> <span class="n">v</span><span class="p">)</span> <span class="ow">in</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">items</span><span class="p">())</span>

        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">_populated</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_populate</span><span class="p">()</span>

        <span class="k">try</span><span class="p">:</span>
            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_is_callback</span><span class="p">(</span><span class="n">lookup_view</span><span class="p">):</span>
                <span class="n">lookup_view</span> <span class="o">=</span> <span class="n">get_callable</span><span class="p">(</span><span class="n">lookup_view</span><span class="p">,</span> <span class="bp">True</span><span class="p">)</span>
        <span class="k">except</span> <span class="p">(</span><span class="ne">ImportError</span><span class="p">,</span> <span class="ne">AttributeError</span><span class="p">)</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
            <span class="k">raise</span> <span class="n">NoReverseMatch</span><span class="p">(</span><span class="s">&quot;Error importing &#39;</span><span class="si">%s</span><span class="s">&#39;: </span><span class="si">%s</span><span class="s">.&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">lookup_view</span><span class="p">,</span> <span class="n">e</span><span class="p">))</span>
        <span class="n">possibilities</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">reverse_dict</span><span class="o">.</span><span class="n">getlist</span><span class="p">(</span><span class="n">lookup_view</span><span class="p">)</span>

        <span class="n">prefix_norm</span><span class="p">,</span> <span class="n">prefix_args</span> <span class="o">=</span> <span class="n">normalize</span><span class="p">(</span><span class="n">urlquote</span><span class="p">(</span><span class="n">_prefix</span><span class="p">))[</span><span class="mi">0</span><span class="p">]</span>
        <span class="k">for</span> <span class="n">possibility</span><span class="p">,</span> <span class="n">pattern</span><span class="p">,</span> <span class="n">defaults</span> <span class="ow">in</span> <span class="n">possibilities</span><span class="p">:</span>
            <span class="k">for</span> <span class="n">result</span><span class="p">,</span> <span class="n">params</span> <span class="ow">in</span> <span class="n">possibility</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">args</span><span class="p">:</span>
                    <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">args</span><span class="p">)</span> <span class="o">!=</span> <span class="nb">len</span><span class="p">(</span><span class="n">params</span><span class="p">)</span> <span class="o">+</span> <span class="nb">len</span><span class="p">(</span><span class="n">prefix_args</span><span class="p">):</span>
                        <span class="k">continue</span>
                    <span class="n">candidate_subs</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">(</span><span class="nb">zip</span><span class="p">(</span><span class="n">prefix_args</span> <span class="o">+</span> <span class="n">params</span><span class="p">,</span> <span class="n">text_args</span><span class="p">))</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="k">if</span> <span class="nb">set</span><span class="p">(</span><span class="n">kwargs</span><span class="o">.</span><span class="n">keys</span><span class="p">())</span> <span class="o">|</span> <span class="nb">set</span><span class="p">(</span><span class="n">defaults</span><span class="o">.</span><span class="n">keys</span><span class="p">())</span> <span class="o">!=</span> <span class="nb">set</span><span class="p">(</span><span class="n">params</span><span class="p">)</span> <span class="o">|</span> <span class="nb">set</span><span class="p">(</span><span class="n">defaults</span><span class="o">.</span><span class="n">keys</span><span class="p">())</span> <span class="o">|</span> <span class="nb">set</span><span class="p">(</span><span class="n">prefix_args</span><span class="p">):</span>
                        <span class="k">continue</span>
                    <span class="n">matches</span> <span class="o">=</span> <span class="bp">True</span>
                    <span class="k">for</span> <span class="n">k</span><span class="p">,</span> <span class="n">v</span> <span class="ow">in</span> <span class="n">defaults</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
                        <span class="k">if</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">k</span><span class="p">,</span> <span class="n">v</span><span class="p">)</span> <span class="o">!=</span> <span class="n">v</span><span class="p">:</span>
                            <span class="n">matches</span> <span class="o">=</span> <span class="bp">False</span>
                            <span class="k">break</span>
                    <span class="k">if</span> <span class="ow">not</span> <span class="n">matches</span><span class="p">:</span>
                        <span class="k">continue</span>
                    <span class="n">candidate_subs</span> <span class="o">=</span> <span class="n">text_kwargs</span>
                <span class="c"># WSGI provides decoded URLs, without %xx escapes, and the URL</span>
                <span class="c"># resolver operates on such URLs. First substitute arguments</span>
                <span class="c"># without quoting to build a decoded URL and look for a match.</span>
                <span class="c"># Then, if we have a match, redo the substitution with quoted</span>
                <span class="c"># arguments in order to return a properly encoded URL.</span>
                <span class="n">candidate_pat</span> <span class="o">=</span> <span class="n">prefix_norm</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s">&#39;%&#39;</span><span class="p">,</span> <span class="s">&#39;</span><span class="si">%%</span><span class="s">&#39;</span><span class="p">)</span> <span class="o">+</span> <span class="n">result</span>
                <span class="k">if</span> <span class="n">re</span><span class="o">.</span><span class="n">search</span><span class="p">(</span><span class="s">&#39;^</span><span class="si">%s%s</span><span class="s">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">prefix_norm</span><span class="p">,</span> <span class="n">pattern</span><span class="p">),</span> <span class="n">candidate_pat</span> <span class="o">%</span> <span class="n">candidate_subs</span><span class="p">,</span> <span class="n">re</span><span class="o">.</span><span class="n">UNICODE</span><span class="p">):</span>
                    <span class="n">candidate_subs</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">((</span><span class="n">k</span><span class="p">,</span> <span class="n">urlquote</span><span class="p">(</span><span class="n">v</span><span class="p">))</span> <span class="k">for</span> <span class="p">(</span><span class="n">k</span><span class="p">,</span> <span class="n">v</span><span class="p">)</span> <span class="ow">in</span> <span class="n">candidate_subs</span><span class="o">.</span><span class="n">items</span><span class="p">())</span>
                    <span class="n">url</span> <span class="o">=</span> <span class="n">candidate_pat</span> <span class="o">%</span> <span class="n">candidate_subs</span>
                    <span class="c"># Don&#39;t allow construction of scheme relative urls.</span>
                    <span class="k">if</span> <span class="n">url</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s">&#39;//&#39;</span><span class="p">):</span>
                        <span class="n">url</span> <span class="o">=</span> <span class="s">&#39;/</span><span class="si">%%</span><span class="s">2F</span><span class="si">%s</span><span class="s">&#39;</span> <span class="o">%</span> <span class="n">url</span><span class="p">[</span><span class="mi">2</span><span class="p">:]</span>
                    <span class="k">return</span> <span class="n">url</span>
        <span class="c"># lookup_view can be URL label, or dotted path, or callable, Any of</span>
        <span class="c"># these can be passed in at the top, but callables are not friendly in</span>
        <span class="c"># error messages.</span>
        <span class="n">m</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">lookup_view</span><span class="p">,</span> <span class="s">&#39;__module__&#39;</span><span class="p">,</span> <span class="bp">None</span><span class="p">)</span>
        <span class="n">n</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">lookup_view</span><span class="p">,</span> <span class="s">&#39;__name__&#39;</span><span class="p">,</span> <span class="bp">None</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">m</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span> <span class="ow">and</span> <span class="n">n</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span>
            <span class="n">lookup_view_s</span> <span class="o">=</span> <span class="s">&quot;</span><span class="si">%s</span><span class="s">.</span><span class="si">%s</span><span class="s">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">m</span><span class="p">,</span> <span class="n">n</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">lookup_view_s</span> <span class="o">=</span> <span class="n">lookup_view</span>

        <span class="n">patterns</span> <span class="o">=</span> <span class="p">[</span><span class="n">pattern</span> <span class="k">for</span> <span class="p">(</span><span class="n">possibility</span><span class="p">,</span> <span class="n">pattern</span><span class="p">,</span> <span class="n">defaults</span><span class="p">)</span> <span class="ow">in</span> <span class="n">possibilities</span><span class="p">]</span>
        <span class="k">raise</span> <span class="n">NoReverseMatch</span><span class="p">(</span><span class="s">&quot;Reverse for &#39;</span><span class="si">%s</span><span class="s">&#39; with arguments &#39;</span><span class="si">%s</span><span class="s">&#39; and keyword &quot;</span>
                <span class="s">&quot;arguments &#39;</span><span class="si">%s</span><span class="s">&#39; not found. </span><span class="si">%d</span><span class="s"> pattern(s) tried: </span><span class="si">%s</span><span class="s">&quot;</span> <span class="o">%</span>
                             <span class="p">(</span><span class="n">lookup_view_s</span><span class="p">,</span> <span class="n">args</span><span class="p">,</span> <span class="n">kwargs</span><span class="p">,</span> <span class="nb">len</span><span class="p">(</span><span class="n">patterns</span><span class="p">),</span> <span class="n">patterns</span><span class="p">))</span>


<span class="k">class</span> <span class="nc">LocaleRegexURLResolver</span><span class="p">(</span><span class="n">RegexURLResolver</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    A URL resolver that always matches the active language code as URL prefix.</span>

<span class="sd">    Rather than taking a regex argument, we just override the ``regex``</span>
<span class="sd">    function to always return the active language-code as regex.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">urlconf_name</span><span class="p">,</span> <span class="n">default_kwargs</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">app_name</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">namespace</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">LocaleRegexURLResolver</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">__init__</span><span class="p">(</span>
            <span class="bp">None</span><span class="p">,</span> <span class="n">urlconf_name</span><span class="p">,</span> <span class="n">default_kwargs</span><span class="p">,</span> <span class="n">app_name</span><span class="p">,</span> <span class="n">namespace</span><span class="p">)</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">regex</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">language_code</span> <span class="o">=</span> <span class="n">get_language</span><span class="p">()</span>
        <span class="k">if</span> <span class="n">language_code</span> <span class="ow">not</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_regex_dict</span><span class="p">:</span>
            <span class="n">regex_compiled</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">compile</span><span class="p">(</span><span class="s">&#39;^</span><span class="si">%s</span><span class="s">/&#39;</span> <span class="o">%</span> <span class="n">language_code</span><span class="p">,</span> <span class="n">re</span><span class="o">.</span><span class="n">UNICODE</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_regex_dict</span><span class="p">[</span><span class="n">language_code</span><span class="p">]</span> <span class="o">=</span> <span class="n">regex_compiled</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_regex_dict</span><span class="p">[</span><span class="n">language_code</span><span class="p">]</span>


<div class="viewcode-block" id="resolve"><a class="viewcode-back" href="../../../ref/urlresolvers.html#django.core.urlresolvers.resolve">[docs]</a><span class="k">def</span> <span class="nf">resolve</span><span class="p">(</span><span class="n">path</span><span class="p">,</span> <span class="n">urlconf</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
    <span class="k">if</span> <span class="n">urlconf</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
        <span class="n">urlconf</span> <span class="o">=</span> <span class="n">get_urlconf</span><span class="p">()</span>
    <span class="k">return</span> <span class="n">get_resolver</span><span class="p">(</span><span class="n">urlconf</span><span class="p">)</span><span class="o">.</span><span class="n">resolve</span><span class="p">(</span><span class="n">path</span><span class="p">)</span>

</div>
<div class="viewcode-block" id="reverse"><a class="viewcode-back" href="../../../ref/urlresolvers.html#django.core.urlresolvers.reverse">[docs]</a><span class="k">def</span> <span class="nf">reverse</span><span class="p">(</span><span class="n">viewname</span><span class="p">,</span> <span class="n">urlconf</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">args</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">kwargs</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">prefix</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">current_app</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
    <span class="k">if</span> <span class="n">urlconf</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
        <span class="n">urlconf</span> <span class="o">=</span> <span class="n">get_urlconf</span><span class="p">()</span>
    <span class="n">resolver</span> <span class="o">=</span> <span class="n">get_resolver</span><span class="p">(</span><span class="n">urlconf</span><span class="p">)</span>
    <span class="n">args</span> <span class="o">=</span> <span class="n">args</span> <span class="ow">or</span> <span class="p">[]</span>
    <span class="n">kwargs</span> <span class="o">=</span> <span class="n">kwargs</span> <span class="ow">or</span> <span class="p">{}</span>

    <span class="k">if</span> <span class="n">prefix</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
        <span class="n">prefix</span> <span class="o">=</span> <span class="n">get_script_prefix</span><span class="p">()</span>

    <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">viewname</span><span class="p">,</span> <span class="n">six</span><span class="o">.</span><span class="n">string_types</span><span class="p">):</span>
        <span class="n">view</span> <span class="o">=</span> <span class="n">viewname</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">parts</span> <span class="o">=</span> <span class="n">viewname</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s">&#39;:&#39;</span><span class="p">)</span>
        <span class="n">parts</span><span class="o">.</span><span class="n">reverse</span><span class="p">()</span>
        <span class="n">view</span> <span class="o">=</span> <span class="n">parts</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
        <span class="n">path</span> <span class="o">=</span> <span class="n">parts</span><span class="p">[</span><span class="mi">1</span><span class="p">:]</span>

        <span class="n">resolved_path</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">ns_pattern</span> <span class="o">=</span> <span class="s">&#39;&#39;</span>
        <span class="k">while</span> <span class="n">path</span><span class="p">:</span>
            <span class="n">ns</span> <span class="o">=</span> <span class="n">path</span><span class="o">.</span><span class="n">pop</span><span class="p">()</span>

            <span class="c"># Lookup the name to see if it could be an app identifier</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="n">app_list</span> <span class="o">=</span> <span class="n">resolver</span><span class="o">.</span><span class="n">app_dict</span><span class="p">[</span><span class="n">ns</span><span class="p">]</span>
                <span class="c"># Yes! Path part matches an app in the current Resolver</span>
                <span class="k">if</span> <span class="n">current_app</span> <span class="ow">and</span> <span class="n">current_app</span> <span class="ow">in</span> <span class="n">app_list</span><span class="p">:</span>
                    <span class="c"># If we are reversing for a particular app,</span>
                    <span class="c"># use that namespace</span>
                    <span class="n">ns</span> <span class="o">=</span> <span class="n">current_app</span>
                <span class="k">elif</span> <span class="n">ns</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">app_list</span><span class="p">:</span>
                    <span class="c"># The name isn&#39;t shared by one of the instances</span>
                    <span class="c"># (i.e., the default) so just pick the first instance</span>
                    <span class="c"># as the default.</span>
                    <span class="n">ns</span> <span class="o">=</span> <span class="n">app_list</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
            <span class="k">except</span> <span class="ne">KeyError</span><span class="p">:</span>
                <span class="k">pass</span>

            <span class="k">try</span><span class="p">:</span>
                <span class="n">extra</span><span class="p">,</span> <span class="n">resolver</span> <span class="o">=</span> <span class="n">resolver</span><span class="o">.</span><span class="n">namespace_dict</span><span class="p">[</span><span class="n">ns</span><span class="p">]</span>
                <span class="n">resolved_path</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">ns</span><span class="p">)</span>
                <span class="n">ns_pattern</span> <span class="o">=</span> <span class="n">ns_pattern</span> <span class="o">+</span> <span class="n">extra</span>
            <span class="k">except</span> <span class="ne">KeyError</span> <span class="k">as</span> <span class="n">key</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">resolved_path</span><span class="p">:</span>
                    <span class="k">raise</span> <span class="n">NoReverseMatch</span><span class="p">(</span>
                        <span class="s">&quot;</span><span class="si">%s</span><span class="s"> is not a registered namespace inside &#39;</span><span class="si">%s</span><span class="s">&#39;&quot;</span> <span class="o">%</span>
                        <span class="p">(</span><span class="n">key</span><span class="p">,</span> <span class="s">&#39;:&#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">resolved_path</span><span class="p">)))</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="k">raise</span> <span class="n">NoReverseMatch</span><span class="p">(</span><span class="s">&quot;</span><span class="si">%s</span><span class="s"> is not a registered namespace&quot;</span> <span class="o">%</span>
                                         <span class="n">key</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">ns_pattern</span><span class="p">:</span>
            <span class="n">resolver</span> <span class="o">=</span> <span class="n">get_ns_resolver</span><span class="p">(</span><span class="n">ns_pattern</span><span class="p">,</span> <span class="n">resolver</span><span class="p">)</span>

    <span class="k">return</span> <span class="n">iri_to_uri</span><span class="p">(</span><span class="n">resolver</span><span class="o">.</span><span class="n">_reverse_with_prefix</span><span class="p">(</span><span class="n">view</span><span class="p">,</span> <span class="n">prefix</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">))</span>
</div>
<span class="n">reverse_lazy</span> <span class="o">=</span> <span class="n">lazy</span><span class="p">(</span><span class="n">reverse</span><span class="p">,</span> <span class="nb">str</span><span class="p">)</span>


<span class="k">def</span> <span class="nf">clear_url_caches</span><span class="p">():</span>
    <span class="n">get_callable</span><span class="o">.</span><span class="n">cache_clear</span><span class="p">()</span>
    <span class="n">get_resolver</span><span class="o">.</span><span class="n">cache_clear</span><span class="p">()</span>
    <span class="n">get_ns_resolver</span><span class="o">.</span><span class="n">cache_clear</span><span class="p">()</span>


<span class="k">def</span> <span class="nf">set_script_prefix</span><span class="p">(</span><span class="n">prefix</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Sets the script prefix for the current thread.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">if</span> <span class="ow">not</span> <span class="n">prefix</span><span class="o">.</span><span class="n">endswith</span><span class="p">(</span><span class="s">&#39;/&#39;</span><span class="p">):</span>
        <span class="n">prefix</span> <span class="o">+=</span> <span class="s">&#39;/&#39;</span>
    <span class="n">_prefixes</span><span class="o">.</span><span class="n">value</span> <span class="o">=</span> <span class="n">prefix</span>


<div class="viewcode-block" id="get_script_prefix"><a class="viewcode-back" href="../../../ref/urlresolvers.html#django.core.urlresolvers.get_script_prefix">[docs]</a><span class="k">def</span> <span class="nf">get_script_prefix</span><span class="p">():</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Returns the currently active script prefix. Useful for client code that</span>
<span class="sd">    wishes to construct their own URLs manually (although accessing the request</span>
<span class="sd">    instance is normally going to be a lot cleaner).</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">return</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">_prefixes</span><span class="p">,</span> <span class="s">&quot;value&quot;</span><span class="p">,</span> <span class="s">&#39;/&#39;</span><span class="p">)</span>

</div>
<span class="k">def</span> <span class="nf">clear_script_prefix</span><span class="p">():</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Unsets the script prefix for the current thread.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">try</span><span class="p">:</span>
        <span class="k">del</span> <span class="n">_prefixes</span><span class="o">.</span><span class="n">value</span>
    <span class="k">except</span> <span class="ne">AttributeError</span><span class="p">:</span>
        <span class="k">pass</span>


<span class="k">def</span> <span class="nf">set_urlconf</span><span class="p">(</span><span class="n">urlconf_name</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Sets the URLconf for the current thread (overriding the default one in</span>
<span class="sd">    settings). Set to None to revert back to the default.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">if</span> <span class="n">urlconf_name</span><span class="p">:</span>
        <span class="n">_urlconfs</span><span class="o">.</span><span class="n">value</span> <span class="o">=</span> <span class="n">urlconf_name</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="k">if</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">_urlconfs</span><span class="p">,</span> <span class="s">&quot;value&quot;</span><span class="p">):</span>
            <span class="k">del</span> <span class="n">_urlconfs</span><span class="o">.</span><span class="n">value</span>


<span class="k">def</span> <span class="nf">get_urlconf</span><span class="p">(</span><span class="n">default</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Returns the root URLconf to use for the current thread if it has been</span>
<span class="sd">    changed from the default one.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">return</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">_urlconfs</span><span class="p">,</span> <span class="s">&quot;value&quot;</span><span class="p">,</span> <span class="n">default</span><span class="p">)</span>


<span class="k">def</span> <span class="nf">is_valid_path</span><span class="p">(</span><span class="n">path</span><span class="p">,</span> <span class="n">urlconf</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Returns True if the given path resolves against the default URL resolver,</span>
<span class="sd">    False otherwise.</span>

<span class="sd">    This is a convenience method to make working with &quot;is this a match?&quot; cases</span>
<span class="sd">    easier, avoiding unnecessarily indented try...except blocks.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">try</span><span class="p">:</span>
        <span class="n">resolve</span><span class="p">(</span><span class="n">path</span><span class="p">,</span> <span class="n">urlconf</span><span class="p">)</span>
        <span class="k">return</span> <span class="bp">True</span>
    <span class="k">except</span> <span class="n">Resolver404</span><span class="p">:</span>
        <span class="k">return</span> <span class="bp">False</span>
</pre></div>

          </div>
        </div>
      </div>
      
        
          <div class="yui-b" id="sidebar">
            
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
  <h3>Browse</h3>
  <ul>
    
    
  </ul>
  <h3>You are here:</h3>
  <ul>
      <li>
        <a href="../../../index.html">Django 1.7.8.dev20150401230226 documentation</a>
        
          <ul><li><a href="../../index.html">Module code</a>
        
          <ul><li><a href="../../django.html">django</a>
        
        <ul><li>django.core.urlresolvers</li></ul>
        </li></ul></li></ul>
      </li>
  </ul>

<div id="searchbox" style="display: none">
  <h3>Quick search</h3>
    <form class="search" action="../../../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
              <h3>Last update:</h3>
              <p class="topless">Apr 02, 2015</p>
          </div>
        
      
    </div>

    <div id="ft">
      <div class="nav">
    <a href="../../index.html" title="Module code" accesskey="U">up</a></div>
    </div>
  </div>

      <div class="clearer"></div>
    </div>
  </body>
</html>